package com.sctlqq.wuye.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sctlqq.wuye.entity.Notice;
import com.sctlqq.wuye.entity.Result;
import com.sctlqq.wuye.service.ISwiperService;
import com.sctlqq.wuye.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 公告Controller（前端控制器）
 */
@RestController
@RequestMapping("/sctlqq/api/wuye/notice")
public class NoticeController {

    @Autowired//自动实例化
    private NoticeService noticeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/getnotice")
    public Result<Object> getNoticeList(){
        return new Result<>(HttpStatus.OK.value(),"success",noticeService.list(new QueryWrapper<Notice>().orderByDesc("time")));
    }

    /**
     * 通过 id  查询单个
     * @param id
     * @return
     */
    @GetMapping("/getnoticebyid")
    public Result<Object> getNoticeById(@RequestParam Long id){
        return new Result<>(HttpStatus.OK.value(),"success",noticeService.getById(id));
    }

    /**
     * 通过给定对象 删除单个
     * @param notice
     * @return
     */
    @DeleteMapping("/deletenotice")
    public Result<Object> deleteNotice(@RequestBody Notice notice){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",noticeService.removeById(notice));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 通过给定对象 新增单个
     * @param notice
     * @return
     */
    @PutMapping("/addnotice")
    public Result<Object> addNotice(@RequestBody Notice notice){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",noticeService.save(notice));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 通过给定对象 修改单个
     * @param notice
     * @return
     */
    @PostMapping("/updatenotice")
    public Result<Object> updateNotice(@RequestBody Notice notice){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",noticeService.updateById(notice));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

}
